package demo.practice.leetcode_eveyday;

import java.util.Arrays;
import java.util.PriorityQueue;

public class P703_2 {

    public static void main(String[] args) {
        KthLargest kthLargest= new P703_2.KthLargest(3, new int[]{6,5,7} );
        System.out.println(kthLargest.add(1));
    }

    static class KthLargest {



        private int k;
        private PriorityQueue<Integer> queue;

        public KthLargest(int k, int[] nums) {
            this.k = k;
            queue = new PriorityQueue<Integer>((i, j) -> i-j);//排序按从  小到大的顺序， 头部（堆顶）更小，尾部（堆底）更大
            for (int i : nums) {
                queue.add(i);
            }

            while (queue.size() > k)
                queue.remove();
        }

        public int add(int val) {
            queue.add(val);
            while (queue.size() > k)
                queue.remove();

            if (queue.size() < k )
                return -1;
            return queue.peek();
        }
    }
}
